package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalHiddenDangerConvert;
import com.quick.develop.business.entity.EnvironmentalHiddenDangerEntity;
import com.quick.develop.business.query.EnvironmentalHiddenDangerQuery;
import com.quick.develop.business.vo.EnvironmentalHiddenDangerVO;
import com.quick.develop.business.dao.EnvironmentalHiddenDangerDao;
import com.quick.develop.business.service.EnvironmentalHiddenDangerService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境隐患排查信息
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalHiddenDangerServiceImpl extends BaseServiceImpl<EnvironmentalHiddenDangerDao, EnvironmentalHiddenDangerEntity> implements EnvironmentalHiddenDangerService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalHiddenDangerVO> page(EnvironmentalHiddenDangerQuery query) {
        IPage<EnvironmentalHiddenDangerEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalHiddenDangerConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalHiddenDangerEntity> getWrapper(EnvironmentalHiddenDangerQuery query) {
        LambdaQueryWrapper<EnvironmentalHiddenDangerEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalHiddenDangerEntity> getWrapperOptional(EnvironmentalHiddenDangerQuery query) {
        LambdaQueryWrapper<EnvironmentalHiddenDangerEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalHiddenDangerEntity::getId, query.getId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalHiddenDangerEntity::getBusinessKey, query.getBusinessKey());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getAttachId()), EnvironmentalHiddenDangerEntity::getAttachId, query.getAttachId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDomain()), EnvironmentalHiddenDangerEntity::getDomain, query.getDomain());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getOverhaulRotation()), EnvironmentalHiddenDangerEntity::getOverhaulRotation, query.getOverhaulRotation());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCheckDate()), EnvironmentalHiddenDangerEntity::getCheckDate, query.getCheckDate());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSpecialInspectionName()), EnvironmentalHiddenDangerEntity::getSpecialInspectionName, query.getSpecialInspectionName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCategory()), EnvironmentalHiddenDangerEntity::getCategory, query.getCategory());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getGrading()), EnvironmentalHiddenDangerEntity::getGrading, query.getGrading());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getFindProblem()), EnvironmentalHiddenDangerEntity::getFindProblem, query.getFindProblem());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getLaw()), EnvironmentalHiddenDangerEntity::getLaw, query.getLaw());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getScenePhoto()), EnvironmentalHiddenDangerEntity::getScenePhoto, query.getScenePhoto());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRectification()), EnvironmentalHiddenDangerEntity::getRectification, query.getRectification());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleUnit()), EnvironmentalHiddenDangerEntity::getResponsibleUnit, query.getResponsibleUnit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleUnitUserCode()), EnvironmentalHiddenDangerEntity::getResponsibleUnitUserCode, query.getResponsibleUnitUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleUnitUserName()), EnvironmentalHiddenDangerEntity::getResponsibleUnitUserName, query.getResponsibleUnitUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getManage()), EnvironmentalHiddenDangerEntity::getManage, query.getManage());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getManageUnitUserCode()), EnvironmentalHiddenDangerEntity::getManageUnitUserCode, query.getManageUnitUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getManageUnitUserName()), EnvironmentalHiddenDangerEntity::getManageUnitUserName, query.getManageUnitUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRectificationLimitTime()), EnvironmentalHiddenDangerEntity::getRectificationLimitTime, query.getRectificationLimitTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRemindNumber()), EnvironmentalHiddenDangerEntity::getRemindNumber, query.getRemindNumber());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getTaskSupervisorStatus()), EnvironmentalHiddenDangerEntity::getTaskSupervisorStatus, query.getTaskSupervisorStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getTaskSupervisorCode()), EnvironmentalHiddenDangerEntity::getTaskSupervisorCode, query.getTaskSupervisorCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getLegislativeReformStatus()), EnvironmentalHiddenDangerEntity::getLegislativeReformStatus, query.getLegislativeReformStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRemark()), EnvironmentalHiddenDangerEntity::getRemark, query.getRemark());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalHiddenDangerEntity::getCreatedUserCode, query.getCreatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalHiddenDangerEntity::getCreatedUserName, query.getCreatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalHiddenDangerEntity::getCreatedTime, query.getCreatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalHiddenDangerEntity::getUpdatedUserCode, query.getUpdatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalHiddenDangerEntity::getUpdatedUserName, query.getUpdatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalHiddenDangerEntity::getUpdatedTime, query.getUpdatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalHiddenDangerEntity::getDelFlag, query.getDelFlag());
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalHiddenDangerEntity::getId, query.getIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalHiddenDangerEntity::getBusinessKey, query.getBusinessKeyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getAttachIdLike()), EnvironmentalHiddenDangerEntity::getAttachId, query.getAttachIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDomainLike()), EnvironmentalHiddenDangerEntity::getDomain, query.getDomainLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getOverhaulRotationLike()), EnvironmentalHiddenDangerEntity::getOverhaulRotation, query.getOverhaulRotationLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCheckDateLike()), EnvironmentalHiddenDangerEntity::getCheckDate, query.getCheckDateLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSpecialInspectionNameLike()), EnvironmentalHiddenDangerEntity::getSpecialInspectionName, query.getSpecialInspectionNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCategoryLike()), EnvironmentalHiddenDangerEntity::getCategory, query.getCategoryLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getGradingLike()), EnvironmentalHiddenDangerEntity::getGrading, query.getGradingLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getFindProblemLike()), EnvironmentalHiddenDangerEntity::getFindProblem, query.getFindProblemLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getLawLike()), EnvironmentalHiddenDangerEntity::getLaw, query.getLawLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getScenePhotoLike()), EnvironmentalHiddenDangerEntity::getScenePhoto, query.getScenePhotoLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRectificationLike()), EnvironmentalHiddenDangerEntity::getRectification, query.getRectificationLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleUnitLike()), EnvironmentalHiddenDangerEntity::getResponsibleUnit, query.getResponsibleUnitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleUnitUserCodeLike()), EnvironmentalHiddenDangerEntity::getResponsibleUnitUserCode, query.getResponsibleUnitUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleUnitUserNameLike()), EnvironmentalHiddenDangerEntity::getResponsibleUnitUserName, query.getResponsibleUnitUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getManageLike()), EnvironmentalHiddenDangerEntity::getManage, query.getManageLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getManageUnitUserCodeLike()), EnvironmentalHiddenDangerEntity::getManageUnitUserCode, query.getManageUnitUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getManageUnitUserNameLike()), EnvironmentalHiddenDangerEntity::getManageUnitUserName, query.getManageUnitUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRectificationLimitTimeLike()), EnvironmentalHiddenDangerEntity::getRectificationLimitTime, query.getRectificationLimitTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRemindNumberLike()), EnvironmentalHiddenDangerEntity::getRemindNumber, query.getRemindNumberLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getTaskSupervisorStatusLike()), EnvironmentalHiddenDangerEntity::getTaskSupervisorStatus, query.getTaskSupervisorStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getTaskSupervisorCodeLike()), EnvironmentalHiddenDangerEntity::getTaskSupervisorCode, query.getTaskSupervisorCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getLegislativeReformStatusLike()), EnvironmentalHiddenDangerEntity::getLegislativeReformStatus, query.getLegislativeReformStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRemarkLike()), EnvironmentalHiddenDangerEntity::getRemark, query.getRemarkLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalHiddenDangerEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalHiddenDangerEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalHiddenDangerEntity::getCreatedTime, query.getCreatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalHiddenDangerEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalHiddenDangerEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalHiddenDangerEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalHiddenDangerEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalHiddenDangerVO vo) {
        EnvironmentalHiddenDangerEntity entity = EnvironmentalHiddenDangerConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalHiddenDangerVO vo) {
        EnvironmentalHiddenDangerEntity entity = EnvironmentalHiddenDangerConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalHiddenDangerVO getEnvironmentalHiddenDangerVO(String id) {
    EnvironmentalHiddenDangerEntity entity = getById(id);
        return EnvironmentalHiddenDangerConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalHiddenDangerVO getEnvironmentalHiddenDangerByBusinessKey(String businessKey) {
        EnvironmentalHiddenDangerEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalHiddenDangerEntity>()
                .eq(EnvironmentalHiddenDangerEntity::getBusinessKey, businessKey));
        return EnvironmentalHiddenDangerConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalHiddenDangerVO> getListByKeys(EnvironmentalHiddenDangerQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalHiddenDangerQuery query) {
        List<EnvironmentalHiddenDangerEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalHiddenDangerEntity.class, list, "环境隐患排查信息导出");
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalHiddenDangerEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalHiddenDangerVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalHiddenDangerEntity> entities = list.stream().map(EnvironmentalHiddenDangerConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalHiddenDangerVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalHiddenDangerEntity> entities = list.stream().map(EnvironmentalHiddenDangerConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalHiddenDangerVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalHiddenDangerConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalHiddenDangerEntity.class).in(key, keys)));
    }

    /**
    *
    * @param sFunction 函数式获取Key
    * @param keys 需要查询的列表
    * @return 返回数据列表
    */
    public List<EnvironmentalHiddenDangerEntity> getListByKeys(SFunction<EnvironmentalHiddenDangerEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalHiddenDangerEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalHiddenDangerEntity> filterDynamicImport(List<EnvironmentalHiddenDangerEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalHiddenDangerEntity> overrideDynamicImport(List<EnvironmentalHiddenDangerEntity> list) {
        return Collections.emptyList();
    }
}